package com.itheima.putige.putige.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
* Description: 公共字段自动填充策略
* @author: zjf95
* @date: 2022/3/1 19:24
* @param:
* @return:
*/
@Slf4j
@Component
public class MyMetaObjecthandler implements MetaObjectHandler {
    /**
    * Description:
    * @author: 插入操作，自动填充
    * @date: 2022/2/8 17:17
    * @param:
    * @return:
     * 插入操作，自动填充
     * @param metaObject
    */
    @Override
    public void insertFill(MetaObject metaObject) {

        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    /**
    * Description:更新操作，自动填充
    * @author:
    * @date: 2022/2/8 17:22
    * @param:
    * @return:
    */
    @Override
    public void updateFill(MetaObject metaObject) {

        log.info("公共字段自动填充[update]...");
        log.info(metaObject.toString());

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", new Long(1));
    }

}
